package com.roc.steelend.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.controller.vo.specialInvoice.SpecialInvoiceByBuyReception;
import com.roc.steelend.controller.vo.specialPay.SpecialByBuyReception;
import com.roc.steelend.pojo.Suppliers;
import com.roc.steelend.service.SuppliersService;
import com.roc.steelend.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin
@Api(tags = "供应商功能接口")
public class SuppliersController {


    @Autowired
    private SuppliersService suppliersService;


    @GetMapping("/v1/supplier/pop/show")
    @ApiOperation(value = "进行分页展示供应商数据(可带条件)",notes = "可以传关键字进行模糊查询")
    public ReturnMsgForTable findClientList(@RequestParam(required = false,value = "searchMsg")@ApiParam(value = "搜索关键字") String searchMsg,
                                            @RequestParam(required = false,value = "now")@ApiParam(value = "当前展示到的页数") Integer now,
                                            @RequestParam(required = false,value = "size")@ApiParam(value = "一次展示多少数据")Integer size) throws InterruptedException {

        if (Objects.equals(searchMsg, "")){
            searchMsg=null;
        }
        List<Suppliers> supplierList = suppliersService.findSupplierList(searchMsg, now, size);
        ReturnMsgForTable msg2 = new ReturnMsgForTable();
        msg2.setObjectList(supplierList);
        msg2.setCurrenPage(now);
        msg2.setTotal(suppliersService.countSupplier(searchMsg));
        return msg2;

    }


    @GetMapping("/v1/supplier/tiaopiao/reconciliation")
    @ApiOperation(value = "关于供应商调账，调票使用的查询",notes = "返回的是供应商的信息")
    public Suppliers supplierTiaoPiaoAndReconciliation(@RequestParam(required = true)Integer supplierId){


        return suppliersService.supplierTiaoPiaoAndReconciliation(supplierId);
    }


    @PostMapping("/v1/post/account/supplier")
    @ApiOperation(value = "供应商界面进行付款接口")
    private ReturnMsg postAccountSupplier(@RequestBody(required = true) SpecialByBuyReception specialByBuyReception){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return suppliersService.postAccountSupplier(specialByBuyReception,Integer.valueOf(employeeIdAsString));
    }

    @PostMapping("/v1/post/invoice/supplier")
    @ApiOperation(value = "供应商界面进行开票接口")
    private ReturnMsg postInvoiceSupplier(@RequestBody(required = true) SpecialInvoiceByBuyReception specialInvoiceByBuyReception){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        System.out.println(specialInvoiceByBuyReception);
        return   suppliersService.postInvoiceSupplier(specialInvoiceByBuyReception,Integer.valueOf(employeeIdAsString));
    }
}
